import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:oramemo/common/config/app_config.dart';
import 'package:oramemo/common/res/gaps.dart';
import 'package:oramemo/common/routes/routes.dart';
import 'package:oramemo/entity/membr_level_entity.dart';
import 'package:oramemo/pages/user/user_router.dart';
import 'package:oramemo/widget/gradient_app_head.dart';
import 'package:oramemo/widget/gradient_background.dart';
import '../../widgets/subscription_feature_table.dart';
import '../../widgets/subscription_plan_button.dart';
import 'logic.dart';
import 'package:get/get.dart';
import 'package:oramemo/common/langs/text_key.dart';

class SubscriptionPage extends StatelessWidget {
  SubscriptionPage({Key? key}) : super(key: key);

  final SubscriptionLogic logic = Get.put(SubscriptionLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          GradientAppHead(
            title: TextKey.subscription.tr,
            actions: IconButton(
              icon: const Icon(AntdIcon.filedone, color: Colors.black54),
              onPressed: () {
                Get.toNamed(UserRouter.consumptionRecord);
              },
            ),
          ),
          Expanded(
            child: GradientBackground(
              child: GetBuilder<SubscriptionLogic>(builder: (logic) {
                List<MembrLevelData> data = logic.controller.memberLevels;
                return SingleChildScrollView(
                  padding: const EdgeInsets.all(16),
                  child: Column(
                    children: [
                      const SizedBox(height: 7),
                      Text(
                        TextKey.subscriptionPro.tr,
                        style: const TextStyle(
                          fontSize: 18,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                      Container(
                        margin: const EdgeInsets.symmetric(vertical: 16),
                        padding: const EdgeInsets.symmetric(
                          horizontal: 12,
                          vertical: 6,
                        ),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(4),
                        ),
                        child: Text(
                          TextKey.subscriptionUpgradeTip.tr,
                          style: const TextStyle(
                            color: Colors.blue,
                            fontSize: 14,
                          ),
                        ),
                      ),
                      const SubscriptionFeatureTable(),
                      const SizedBox(height: 24),
                      for (MembrLevelData item in data)
                        Container(
                          padding: const EdgeInsets.only(bottom: 12),
                          child: SubscriptionPlanButton(
                            duration: item.memberLevelDesc?.levelTitle ?? '',
                            price: '¥${item.levelFee}',
                            isCheck: item.id == logic.checkData?.id,
                            onTap: () {
                              logic.checkData = item;
                              logic.update();
                            },
                          ),
                        ),
                      const SizedBox(height: 20),
                      ElevatedButton(
                        onPressed: () {
                          logic.createOrder();
                        },
                        style: ElevatedButton.styleFrom(
                          backgroundColor: Colors.blue,
                          minimumSize: const Size(double.infinity, 50),
                          elevation: 0,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(12),
                          ),
                        ),
                        child: Text(
                          TextKey.subscriptionPurchase.tr,
                          style: const TextStyle(fontSize: 16),
                        ),
                      ),
                      const SizedBox(height: 5),
                      Row(
                        children: [
                          Checkbox(
                            value: logic.isAgreed,
                            onChanged: (value) {
                              logic.isAgreed = value ?? false;
                              logic.update();
                            },
                            shape: const CircleBorder(),
                          ),
                          Expanded(
                            child: Padding(
                              padding: const EdgeInsets.only(top: 8.0),
                              child: RichText(
                                text: TextSpan(
                                  style:
                                      const TextStyle(color: Color(0xFF999999)),
                                  children: [
                                    TextSpan(text: TextKey.subscriptionAgreementPrefix.tr),
                                    TextSpan(
                                        text: TextKey.subscriptionTerms.tr,
                                        style:
                                            TextStyle(color: Colors.blue[700]),
                                        recognizer: TapGestureRecognizer()
                                          ..onTap = () {
                                            Get.toNamed(Routers.webView,
                                                arguments: {
                                                  'title': TextKey.subscriptionTerms.tr,
                                                  'url': AppConfig.webUrl
                                                });
                                          }),
                                    TextSpan(text: TextKey.subscriptionAnd.tr),
                                    TextSpan(
                                        text: TextKey.subscriptionServiceAgreement.tr,
                                        style:
                                            TextStyle(color: Colors.blue[700]),
                                        recognizer: TapGestureRecognizer()
                                          ..onTap = () {
                                            Get.toNamed(Routers.webView,
                                                arguments: {
                                                  'title': TextKey.subscriptionServiceAgreement.tr,
                                                  'url': AppConfig.webUrl
                                                });
                                          }),
                                  ],
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                      Gaps.vGap32
                    ],
                  ),
                );
              }),
            ),
          ),
        ],
      ),
    );
  }
}
